package com.juncdt.countdown.controller;

import com.juncdt.countdown.util.R;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by mikemhm 2021/4/16.
 */

@RestController
@RequestMapping("/bigNewScreen")
public class BigNewScreen {
    int timeLeng = 150*6*6;//正计时分钟数
    String timeFormat = "01:30:00";//正计时终点
    String isStart = "0";//0是未开赛，1是开赛,2结束
    Long startTime = -1L;//已开赛时间秒数
    Long nostartTime = -1L;//未开赛时间秒数
    String startTime1 = "00:00:00";//已开赛时间正计时
    String noStartTime1 = "00:00:00";//未开赛时间倒计时
    Long l = 0L;//当前时间到开赛时间的秒数
    ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1, new BasicThreadFactory.Builder().namingPattern("example-schedule-pool-%d").daemon(true).build());
    ScheduledFuture future = null;
    Date parse = null;//开赛时间

    /**
     * 未开赛，返回倒计时；开赛返回正计时
     * @return
     */
    @GetMapping("/time")
    public R returnTime() {
        if ("0".equals(isStart)) {
            return R.ok(noStartTime1).put("type",isStart);
        } else {
            return R.ok(startTime1).put("type",isStart);
        }
    }

    /**
     * 未开赛，返回 "00:00:00" ;开赛返回正计时
     * @return
     */
    @GetMapping("/time2")
    public R returnTime2() {
        if ("0".equals(isStart)) {
            return R.ok("00:00:00").put("type",isStart);
        } else {
            return R.ok(startTime1).put("type",isStart);
        }
    }

    @GetMapping("/setTime")
    public R setTime(String ksTime,String jsTime) {
        if (StringUtils.isEmpty(ksTime) || StringUtils.isEmpty(jsTime)) {
            return R.error("参数缺失");
        }
        if (future != null) {
            //失效上次定时器
            future.cancel(true);
        }
        startTime = -1L;
        nostartTime = -1L;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            parse = format.parse(ksTime);
            Date jstime = format.parse(jsTime);

            timeLeng = (int)(jstime.getTime()-parse.getTime())/1000/60;//正计时分钟数
            setTimeFormat();//正计时终点
        } catch (ParseException e) {
            e.printStackTrace();
        }
        l = (System.currentTimeMillis() - parse.getTime()) / 1000;
        future = executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (l < 0) {
                    //还未开赛
                    isStart = "0";
                    if (nostartTime == -1) {
                        nostartTime = -l;
                    }
                    if (nostartTime > 0) {
                        nostartTime--;
                        long hh = nostartTime / 60 / 60;
                        long mm = nostartTime / 60 % 60;
                        long ss = nostartTime % 60;
                        String Hh = hh < 10 ? "0" + hh : "" + hh;
                        String Mm = mm < 10 ? "0" + mm : "" + mm;
                        String Ss = ss < 10 ? "0" + ss : "" + ss;
                        noStartTime1 = Hh + ":" + Mm + ":" + Ss;
                    } else {
                        l = (System.currentTimeMillis() - parse.getTime()) / 1000;
                    }
                    System.out.println("倒计时："+noStartTime1);
                } else {
                    //已开赛
                    isStart = "1";
                    if (l > timeLeng*60||startTime>=timeLeng*60) {
                        isStart="2";
                        startTime1 = timeFormat;
                        if (future != null) {
                            future.cancel(true);
                            future = null;
                        }
                    } else {
                        if (startTime == -1) {
                            startTime = l;
                        }
                        startTime++;
                        long hh = startTime / 60 / 60;
                        long mm = startTime / 60 % 60;
                        long ss = startTime % 60;
                        String Hh = hh < 10 ? "0" + hh : "" + hh;
                        String Mm = mm < 10 ? "0" + mm : "" + mm;
                        String Ss = ss < 10 ? "0" + ss : "" + ss;
                        startTime1 = Hh + ":" + Mm + ":" + Ss;
                    }
                    System.out.println("正计时："+startTime1);
                }
            }
        }, 0, 1, TimeUnit.SECONDS);
        return R.ok();
    }

    private void setTimeFormat() {
        int i = timeLeng * 60;
        long hh = i / 60 / 60;
        long mm = i / 60 % 60;
        long ss = i % 60;
        String Hh = hh < 10 ? "0" + hh : "" + hh;
        String Mm = mm < 10 ? "0" + mm : "" + mm;
        String Ss = ss < 10 ? "0" + ss : "" + ss;
        timeFormat = Hh + ":" + Mm + ":" + Ss;
    }

    public static void main(String[] args) {
        int nostartTime = 3600;
        long hh = nostartTime / 60 / 60 % 60;
        long mm = nostartTime / 60 % 60;
        long ss = nostartTime % 60;
        String Hh = hh < 10 ? "0" + hh : "" + hh;
        String Mm = mm < 10 ? "0" + mm : "" + mm;
        String Ss = ss < 10 ? "0" + ss : "" + ss;
        String t = Hh + ":" + Mm + ":" + Ss;
        System.out.println(t);
    }

}
